package com.cgtong.android.mvp.presenter;


import android.text.TextUtils;

import com.blankj.utilcode.util.ToastUtils;
import com.cgtong.android.bean.ActivityBean;
import com.cgtong.android.bean.BannerBean;
import com.cgtong.android.mvp.contract.WonderfulActivityContract;
import com.cgtong.android.mvp.model.WonderfulActivityModel;
import com.zhouyou.http.callback.SimpleCallBack;
import com.zhouyou.http.exception.ApiException;

import java.util.List;

/**
 * Created by Sky.Bing on 2017/10/24.
 */

public class WonderfulActivityPresenter implements WonderfulActivityContract.Presenter {
    private WonderfulActivityContract.View mView;
    private WonderfulActivityContract.Model mModel;

    public WonderfulActivityPresenter(WonderfulActivityContract.View view) {
        this.mView = view;
        mModel = new WonderfulActivityModel();
    }

    @Override
    public void getActivityList(int maxId) {
        mView.setEnableLoadMore(false);
        mModel.getActivityList(maxId, new SimpleCallBack<List<List<ActivityBean>>>() {
            @Override
            public void onError(ApiException e) {
                mView.onLoadError();
            }

            @Override
            public void onSuccess(List<List<ActivityBean>> activityBeanLists) {
                mView.showActivityList(true, ActivityBean.parseJsonOfDoubleList(activityBeanLists));
                mView.setEnableLoadMore(true);
                mView.setRefreshing(false);
            }
        });
    }

    @Override
    public void loadMoreActivity(int maxId) {
        mModel.getActivityList(maxId, new SimpleCallBack<List<List<ActivityBean>>>() {
            @Override
            public void onError(ApiException e) {
                if (!TextUtils.isEmpty(e.getMessage())) {
                    ToastUtils.showLong(e.getMessage());
                    mView.onLoadMoreError();
                }
            }

            @Override
            public void onSuccess(List<List<ActivityBean>> activityBeanLists) {
                mView.showActivityList(false, ActivityBean.parseJsonOfDoubleList(activityBeanLists));
                mView.setRefreshing(false);
            }
        });
    }

    @Override
    public void getBannerList() {
        mModel.getBannerList(new SimpleCallBack<List<BannerBean>>() {
            @Override
            public void onError(ApiException e) {
            }

            @Override
            public void onSuccess(List<BannerBean> bannerBeen) {
                mView.showBanner(bannerBeen);
            }
        });
    }
}